package com.xiao.user.controller;

import com.xiao.user.model.User;
import com.xiao.user.model.dto.UserDto;
import com.xiao.user.service.criteria.UserCriteria;
import com.xiao.user.service.user.UserService;
import com.xiao.user.web.util.PaginationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Slf4j
//@Api(value = "角色", tags = "角色")
public class UserController extends AbstractResource<User, UserDto, Integer>{

    private final UserService userService;
    public static final String ENTITY_NAME = "User";
    public UserController(UserService userService) {
        super(userService, ENTITY_NAME);
        this.userService = userService;
    }


//    @ApiOperation(value = "新建")
    @PostMapping
    public ResponseEntity<UserDto> save(@RequestBody UserDto dto) {
        return ResponseEntity.ok(userService.create(dto));
    }

//    @ApiOperation(value = "根据id删除角色", notes = "根据id删除角色")
    @DeleteMapping("{id}")
//    @CachePut(cacheNames = "role:permission", key = "#id")
    public ResponseEntity<Void> deleteById(@PathVariable(name = "id") Integer id) {
        return deleteEntity(id);
    }


//    @ApiOperation(value = "更新角色", notes = "更新角色")
    @PutMapping("{id}")
//    @CacheEvict(cacheNames = "role:permission", key = "#id")
    public ResponseEntity<UserDto> update(
            @PathVariable("id") Integer id,
            @Validated @RequestBody UserDto dto) {
        return ResponseEntity.ok(userService.update(id, dto));
    }

    @GetMapping("{id}")
//    @ApiOperation("根据ID查询")
    public ResponseEntity<UserDto> findById(@PathVariable("id") Integer id) {
        return findEntityById(id);
    }

    @GetMapping
//    @ApiOperation("查询所有数据")
    public ResponseEntity<List<UserDto>> findAll(UserCriteria criteria, Pageable pageable) throws InterruptedException {
        return findAllByPage(criteria, pageable);
    }

    @GetMapping("all")
//    @ApiOperation("查询所有数据")
    public ResponseEntity<List<UserDto>> findAll() {
        final List<UserDto> UserDtoList = userService.findAll();
        HttpHeaders headers = PaginationUtil.generateListHttpHeaders(UserDtoList);
        return ResponseEntity.ok().headers(headers).body(UserDtoList);
    }

    @Override
    protected Integer getId(UserDto dto) {
        return dto.getId();
    }
}
